﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraNavBar;
using DevExpress.XtraTreeList;
using MM.Commmon.UI;
using MM.Common.Base;
using MM.Common.DBBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmGeneralDictionarySetup : FrmFunctionBase
    {
        public FrmGeneralDictionarySetup()
        {
            InitializeComponent();
        }
        private void EditItem(int id)
        {
            if (string.IsNullOrEmpty(this.dictCode))
            {
                XtraMessageBox.Show("请选择您要新增字段的表！");
            }
            else
            {
                FrmGeneralDictionaryItemSetup setup = new FrmGeneralDictionaryItemSetup(this.dictCode, this.dictName, id);
                if (setup.ShowDialog() == DialogResult.OK)
                {
                    if (id < 0)
                    {
                        id = setup.DictionaryID;
                    }
                    this.FreshItem(id);
                }
                setup.Dispose();
            }
        }

        private void EditTable(string dict_type)
        {
            FrmGeneralDictionaryTableSetup setup = new FrmGeneralDictionaryTableSetup(base.FunctionParameter, dict_type);
            if (setup.ShowDialog() == DialogResult.OK)
            {
                this.FreshTable(setup.TableCode);
            }
            setup.Dispose();
        }

        private void FreshItem(int id)
        {
            string commandtext = "SELECT * FROM MXHLY_SYS_DICTIONARY WHERE DT_TYPE = :tab ORDER BY DT_SORT ,DT_CODE";
            DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("tab", this.dictCode) });
            DataTable dt = new DataTable();
            if (SystemInfo.DbData.Retrieve(commandtext, parm, ref dt))
            {
                this.treeDictionary.ClearNodes();
                this.treeDictionary.DataSource = dt;
                if (id > 0)
                {
                    for (int i = 0; i < this.treeDictionary.Nodes.Count; i++)
                    {
                        if (id.ToString() == this.treeDictionary.Nodes[i].GetValue("DT_ID").ToString())
                        {
                            this.treeDictionary.FocusedNode = this.treeDictionary.Nodes[i];
                            return;
                        }
                    }
                }
            }
        }

        private void FreshTable(string default_code)
        {
            string commandtext = "SELECT * FROM MXHLY_SYS_DICTIONARY_TABLE WHERE DT_TB_GROUP = :groupcode ORDER BY DT_TB_DESC ,DT_TB_TABLE_CODE";
            DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("groupcode", base.FunctionParameter) });
            DataTable dt = new DataTable();
            if (SystemInfo.DbData.Retrieve(commandtext, parm, ref dt))
            {
                this.GridDicList.DataSource = dt;
            }
            if (!string.IsNullOrEmpty(default_code))
            {
                int dataSourceIndex = -1;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["DT_TB_TABLE_CODE"].ToString() == default_code)
                    {
                        dataSourceIndex = i;
                        break;
                    }
                }
                this.GridViewDicList.FocusedRowHandle = this.GridViewDicList.GetRowHandle(dataSourceIndex);
            }
        }

        private void GridViewDicList_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            DataRow focusedDataRow = this.GridViewDicList.GetFocusedDataRow();
            if (focusedDataRow != null)
            {
                string str = focusedDataRow["DT_TB_TABLE_CODE"].ToString();
                string str2 = focusedDataRow["DT_TB_DISPLAY_NAME"].ToString();
                focusedDataRow["DT_TB_MODIFY"].ToString();
                this.dictCode = str;
                this.dictName = str2;
                this.FreshItem(-1);
            }
        }

        public override bool Init()
        {
            if (SystemInfo.DbData.IsOpen)
            {
                object obj2 = SystemInfo.DbData.GetValue("MXHLY_SYS_DICTIONARY", "DT_ID", base.FunctionParameter, "DT_NAME");
                if (obj2 is string)
                {
                    this.Text = obj2.ToString() + "字典设置";
                }
                this.FreshTable("");
            }
            return base.Init();
        }
        private void nbiDelete_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            if (XtraMessageBox.Show("是否真的删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                GeneralDictionary.DeleteDict(this.treeDictionary.FocusedNode.GetDisplayText("DT_ID").Trim());
                this.FreshItem(-1);
            }
        }

        private void nbiDeleteTable_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            if (this.treeDictionary.Nodes.Count > 0)
            {
                XtraMessageBox.Show("还有项目不能删除表！", "提示");
            }
            else if (XtraMessageBox.Show("是否真的删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                GeneralDictionary.DeleteTableDict(this.dictCode);
                this.FreshTable("");
            }
        }

        private void nbiEdit_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            string str = this.treeDictionary.FocusedNode.GetDisplayText("DT_ID").Trim();
            if (str.IsInt())
            {
                this.EditItem(int.Parse(str));
            }
        }

        private void nbiEditTable_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.EditTable(this.dictCode);
        }

        private void nbiNew_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.EditItem(-1);
        }

        private void nbiNewTable_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.EditTable(null);
        }

        private void nbiPrint_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            //if (PrintHelper.IsPrintingAvailable)
            //{
            //    PrintHelper.ShowPreview(this.treeDictionary);
            //}
        }

        private void ShowHitInfo(TreeListHitInfo hi)
        {
            if (hi.Node != null)
            {
                this.checkNode = hi.Node.GetValue("DT_ID").ToString();
            }
        }

        private void treeDictionary_DoubleClick(object sender, EventArgs e)
        {
            string str = this.treeDictionary.FocusedNode.GetDisplayText("DT_ID").Trim();
            if (str.IsInt())
            {
                this.EditItem(int.Parse(str));
            }
        }

        private void treeDictionary_MouseDown(object sender, MouseEventArgs e)
        {
            this.ShowHitInfo(this.treeDictionary.CalcHitInfo(new Point(e.X, e.Y)));
        }
    }
}
